var test = {}

var BASEDATA = [1,2,4,1,23,4,3,5,1,3];
var TAT_STR = "i was watching u";
test.is_run = function(){
    return true;
}

/* 冒泡排序 */
test.mappao = function(){

    var dlen = BASEDATA.length;

    var sidx;
    var cidx;
    cc.log(BASEDATA, "----->>befor") 
    var ss = 0;
    for (var i=0; i<dlen; i++){
        ss++;
        for (var j=0; j<dlen-1; j++)
        { 
            sidx = BASEDATA[j];
            cidx = BASEDATA[j + 1];
            if (sidx > cidx){
                BASEDATA[j] = cidx;
                BASEDATA[j+1] = sidx;
            }   
        }
    }
    cc.log(BASEDATA, "----->>end")
}

/* 快速排序 */
test.quick_sort = function(arr){
    var arcnt = arr.length;
    if (arcnt <= 1){
        return arr;
    }
    
    var lar = [];
    var rar = [];
    var cmpnum = arr[0];
    for (var i=1; i<arcnt; i++){
        if (arr[i]<cmpnum){
            lar.push(arr[i])
        }
        else{
            rar.push(arr[i])
        }
    }
    return [].concat(test.quick_sort(lar), [cmpnum], test.quick_sort(rar));
}


/* 取最多出现次数 */
test.max_exist = function(){
    var tmpdic = {};
    var dlen = BASEDATA.length;

    var tmpkey;
    var idx = 0;
    for (var i=0; i<dlen; i++){
        idx++;
        tmpkey = BASEDATA[i]
        if (!tmpdic[tmpkey]){
            tmpdic[tmpkey] = 1;
        }
        else{
            tmpdic[tmpkey]++;
        }
    }
    cc.log(idx);
    cc.log(tmpdic, '---->>exist');
}


/* 字符串取反 */
test.str_revert = function(){
    cc.log(TAT_STR.length);
    cc.log(TAT_STR.split(" "));
    var splist = TAT_STR.split(" ");
    var ret = "";
    for (var i=splist.length-1; i>=0; i--)
    {
        cc.log(i , "-->>i")
        ret += splist[i];
    }
    cc.log(ret, '--->>ret');

    var ret2 = splist.reverse().join(" ")
    cc.log(ret2, '--->>ret2')
    
}   

/* js oop测试 */
test.oop_test = function(){
    // 定义一个动物类
    function Animal (name) {
        // 属性
        this.name = name || 'Animal';
        // 实例方法
        this.sleep = function(){
        cc.log(this.name + 'sleep now');
        }
    }
    // 原型方法
    Animal.prototype.eat = function(food) {
        cc.log(this.name + 'eating' + food);
    };

    function Cat(){ 
    }
    Cat.prototype = new Animal();
    Cat.prototype.name = 'cat';

    //　Test Code
    var cat = new Cat();
    cc.log(cat.name);
    cc.log(cat.eat('fish'));
    cc.log(cat.sleep());
    cc.log(cat instanceof Animal); //true 
    cc.log(cat instanceof Cat); //true


    function Person(){
        
    }
    
    Person.prototype.common = {
    id: 1
    };
    
    var p1 = new Person();
    var p2 = new Person();
    
    p1.common.id = 'p1';
    console.log(p1.common.id, p2.common.id);
    
    p2.common.id = 'p2';
    console.log(p1.common.id, p2.common.id);
}

/* js json解析测试 */
test.jsonparse_test = function(){
    cc.loader.loadJson("src/fight/TEMPLATE_DATA.json", function(error, ret2){
        if (error){
            cc.log("error");
            return;
        }
        cc.log(ret2, '--->>ret2')
    })
    // cc.log(ret, "---->>ret");
}

/* js 获取系统赶时间 */
test.timeget = function(){
    var ntime = test.timeget()
    cc.log(ntime, '--->>test nowtime')
}

/* js logger系统 */
test.logger_test = function(){
    var logger = new Logger();
    var webhandler = new WebHandler();
    logger.addHandler(webhandler);
}